<?php
/*
Plugin Name: StumbleUpon Hot Status Indicator
Version: 0.3
Plugin URI: http://leau.net/su/
Author: Edward de Leau
Author URI: http://edward.de.leau.net
Description: Places a grey or colored stumble-upon icon and link behind each url

=>> Visit the plugin's homepage for more information and latest updates  <<=

Installation:

1. Download the file and the su.gif file
2. Activate the plugin from your WordPress admin 'Plugins' page.

*/
/*
Copyright (c) 2006-2008 by Edward de Leau (aka Cogmios aka Edelwater)

v.0.1 primary version
v.0.2 skipped .zip, .pdf
v.0.3 added colors to the icon: grey if not in su, colored if it is

Thanks to the php manual http://nl2.php.net/manual/en/function.strstr.php !

*/


//strxchr(string haystack, string needle [, bool int leftinclusive [, bool int rightinclusive ]])
function strxchr($haystack, $needle, $l_inclusive = 0, $r_inclusive = 0){
   //Note our use of !==. Now work if the position of $needle in $haystack was the 0th (first) character.
   if(strrpos($haystack, $needle) !== false){
       //Everything before last $needle in $haystack.
       $left =  substr($haystack, 0, strrpos($haystack, $needle) + $l_inclusive);
       //Switch value of $r_inclusive from 0 to 1 and viceversa.
       $r_inclusive = ($r_inclusive == 0) ? 1 : 0;
       //Everything after last $needle in $haystack.
       $right =  substr(strrchr($haystack, $needle), $r_inclusive);
       //Return $left and $right into an array.
       $a = array($left, $right);
       return $a;
   }else{
       return false;
   }
}


function Replacer($matches) {
    $or_string  = '<a href="' . $matches[1] . '">' . $matches[2] . '</a>';
    $st_string  = '<a href="http://www.stumbleupon.com/url/' . $matches[1] . '">' .

    $st_string  = $st_string .  '<script src="http://leau.net/su/su.php?' .  $matches[1] . '" type="text/javascript"></script>';

    $st_string  = $st_string . '</a>';

    // if the string contains any html tags we'll skip it, doesnt look good
    if (strstr ( $matches[2], '<' )) {
      return $or_string;
    } else {
      // if the url is a zip, pdf, etc... also skip it
      $foundstr = strxchr($matches[1], ".", 1, 1);
      if ($foundstr && (stristr('.zip .pdf .arj', $foundstr[1]) != FALSE)) {
            return $or_string;
      }
      return $or_string . $st_string;
    }
}

function add_stumble_icon ($text) {
    return preg_replace_callback('/<a href="(.*?)">(.*?)<\\/a>/i', 'Replacer', $text);
}

add_filter('the_content', 'add_stumble_icon', 9);
?>
